package com.kakaxi.mybilibili.module.home.find.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kakaxi.mybilibili.bean.HorizontalVideoItemBean;
import com.kakaxi.mybilibili.common.WebParamConst;
import com.kakaxi.mybilibili.module.home.find.model.IFindRankModel;
import com.kakaxi.mybilibili.module.home.find.model.FindRankModel;
import com.kakaxi.mybilibili.module.home.find.view.IFindRankView;
import com.vincestyling.netroid.Listener;
import com.vincestyling.netroid.NetroidError;

import java.util.List;

/**
 * 排行榜控制层实现类
 * Created by kakaxi on 16/11/25.
 */

public class FindRankPresenter implements IFindRankPresenter {

    private IFindRankView mView;

    private IFindRankModel mModel;

    public FindRankPresenter(IFindRankView rankView) {
        mView = rankView;
        mModel = new FindRankModel();
    }

    @Override
    public void initRankData(int rankType, int tabType) {
        mView.showLoading();
        getRankData(rankType, tabType);
    }

    @Override
    public void refreshRankData(int rankType, int tabType) {
        getRankData(rankType, tabType);
    }

    private void getRankData(int rankType, int tabType) {
        mModel.getRankData(rankType, tabType, new Listener<JSONObject>() {
            @Override
            public void onSuccess(JSONObject response) {
                super.onSuccess(response);
                mView.hideLoading();
                JSONObject rankJsonObj = response.getJSONObject(WebParamConst.RESPONSE_DATA_FIND_RANK);
                JSONArray rankJsonArr = rankJsonObj.getJSONArray(WebParamConst
                        .RESPONSE_DATA_FIND_RANK_LIST);
                List<HorizontalVideoItemBean> horizontalVideoItemBeanList = JSON.parseArray
                        (rankJsonArr.toString(), HorizontalVideoItemBean.class);
                mView.showRankData(horizontalVideoItemBeanList);
            }

            @Override
            public void onError(NetroidError error) {
                super.onError(error);
                mView.hideLoading();
            }
        });
    }

}
